/**
 * The Community Project
 * Copyright Team1 Productions
 * 
 * Joshua Amick
 * Stephen Cunningham
 * Thema Davis
 * Tomika Dawson
 * Lucie DeCristofaro
 * 
 * All Rights Reserved.
 */

package com.actions;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.domain.Announcement;
import com.domain.Email;
import com.domain.Event;
import com.domain.User;
import com.forms.CreateAnnOrEventForm;
import com.forms.ForwardForm;
import com.forms.ProfileDateForm;

public class ForwardAction extends CommunityBaseAction {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		super.execute(mapping, form, request, response);

		// Cast the actionForm, if there is one.
		ForwardForm f = (ForwardForm) form;
		// Business Logic
		//// 
		//// simple forwarding action to whatever url is provided
		
		
		//Go to the main page, load data for display
		if(f.getLoc().equals("main")){
			List<Event> events = this.biz.getPrivateMgr().getTopEvents();
			List<Announcement> announcements = this.biz.getPrivateMgr().getTopAnnouncements();
			
			request.setAttribute("EVENTS", events);
			request.setAttribute("ANNOUNCEMENTS", announcements);
		}
		
//		Go to the creat user page, load data for display
		if(f.getLoc().equals("createUser")&&f.getObjKeyId()!=null){
			User user = this.biz.getPrivateMgr().getUserByKeyId(Long.parseLong(f.getObjKeyId()));
			if(user!=null){			
				request.setAttribute("RESULT_USER", user);
				request.setAttribute("DATES", getDates(user));
			}
		}
		
//		Go to the approve page, load data for display
		if(f.getLoc().equals("approve")){
			List<User> userResults = this.biz.getPrivateMgr().getPendingUsers();
			if(userResults!=null&&userResults.size()>0)request.setAttribute("APPROVE_USERS", userResults);
			List<Announcement> announcementResults = this.biz.getPrivateMgr().getPendingAnnouncements();
			if(announcementResults!=null&&announcementResults.size()>0)request.setAttribute("APPROVE_ANNOUNCEMENTS", announcementResults);
			List<Event> eventResults = this.biz.getPrivateMgr().getPendingEvents();
			if(eventResults!=null&&eventResults.size()>0)request.setAttribute("APPROVE_EVENTS", eventResults);
			List<Email> emailResults = this.biz.getPrivateMgr().getPendingEmails();
			if(emailResults!=null&&emailResults.size()>0)request.setAttribute("APPROVE_EMAILS", emailResults);
		}
		
//		Go to the manage events and announcements page, load data for display
		if(f.getLoc().equals("manage")){
			List<Announcement> announcementResults = this.biz.getPrivateMgr().getAllUndeletedAnnouncements();
			if(announcementResults!=null&&announcementResults.size()>0)request.setAttribute("ANNOUNCEMENTS", announcementResults);
			List<Event> eventResults = this.biz.getPrivateMgr().getAllUndeletedEvents();
			if(eventResults!=null&&eventResults.size()>0)request.setAttribute("EVENTS", eventResults);
		}
		
//		Go to the create an announcement or event page, load data for display
		if(f.getLoc().equals("createAnnouncement")){
			if(f.getObjKeyId()!=null&&f.getObjKeyId().length()>0){
				Long keyId = Long.parseLong(f.getObjKeyId());
				CreateAnnOrEventForm newForm = new CreateAnnOrEventForm();
				if(f.getType().equals("event")){
					Event event = this.biz.getPrivateMgr().getEventByKeyId(keyId);
					if(event!=null){
						newForm.parseEvent(event);
						request.setAttribute("FORM", newForm);
					}
				}
				else{
					Announcement announcement = this.biz.getPrivateMgr().getAnnouncementByKeyId(keyId);
					if(announcement!=null){
						newForm.parseAnnouncement(announcement);
						request.setAttribute("FORM", newForm);
					}
				}
			}
			request.setAttribute("EDIT", "edit");
		}
		
		//deleting an object then returning to proper page
		if(f.getLoc().equals("delete")){
			if(f.getObjKeyId()!=null&&f.getObjKeyId().length()>0){
				Long keyId = Long.parseLong(f.getObjKeyId());
				if(f.getType().equals("user")){
					User user = this.biz.getPrivateMgr().getUserByKeyId(keyId);
					if(user!=null){
						user.setDeleted(true);
						user = this.biz.getPrivateMgr().getUserDAO().save(user);
					}
					List<User> results = this.biz.getPrivateMgr().getAllApprovedUsers();
					User usr = (User)request.getSession().getAttribute("AUTH_USER");
					if(usr!=null){
						for(int i = 0;i<results.size();i++){
							if(results.get(i).getKeyId().equals(usr.getKeyId())){
								results.remove(i);
							}
						}
					}
					request.setAttribute("SEARCH_RESULTS", results);
					request.setAttribute("INFO", user.getUsername()+" was successfully deleted.");
					f.setLoc("searchUsers");
				}
				else if(f.getType().equals("event")){
					
				}
				else if(f.getType().equals("announcement")){
					
				}
			}
		}
		
//		Go to the view profile page, load data for display
		if(f.getLoc().equals("viewProfile")){
			User userToView = this.biz.getPrivateMgr().getUserByKeyId(Long.parseLong(f.getObjKeyId()));
			if(userToView!=null){				
				request.setAttribute("USER", userToView);
				request.setAttribute("DATES", getDates(userToView));
			}
		}
		
//		Go to the search profile or users page, load data for display
		if(f.getLoc().equals("searchUsers")||f.getLoc().equals("searchProfile")){
			List<User> results = this.biz.getPrivateMgr().getAllApprovedUsers();
			User usr = (User)request.getSession().getAttribute("AUTH_USER");
			if(usr!=null){
				for(int i = 0;i<results.size();i++){
					if(results.get(i).getKeyId().equals(usr.getKeyId())){
						results.remove(i);
					}
				}
			}
			if(f.getLoc().equals("searchUsers"))request.setAttribute("controls", "admin");
			else request.setAttribute("controls", "public");
			request.setAttribute("SEARCH_RESULTS", results);
		}
		

		return mapping.findForward(f.getLoc());//forward;
	}
	
	//Date formating for user profiles
	private ProfileDateForm getDates(User user)
	{
		ProfileDateForm fDate = new ProfileDateForm();
		if (user.getProfile()!=null)
		{
			if (user.getProfile().getEmploymentStartDate()!=null)
			{
				String date = user.getProfile().getEmploymentStartDate().toString();
				fDate.setEmpStartYear(date.substring(0, 4));
				fDate.setEmpStartMonth(date.substring(5, 7));
				fDate.setEmpStartDay(date.substring(8, 10));
			}
			if (user.getProfile().getGraduationDate()!=null)
			{
				String date = user.getProfile().getGraduationDate().toString();				
				fDate.setGradYear(date.substring(0, 4));
				fDate.setGradMonth(date.substring(5, 7));
				fDate.setGradDay(date.substring(8, 10));
			}
		}
		
		return fDate;
	}
}
